﻿#include "MarkDialog.h"
#include "ui_MarkDialog.h"

#include <QClipboard>
#include <QDir>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
MarkDialog::MarkDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MarkDialog)
{
    ui->setupUi(this);
    m_count = 1;
    m_dir = "";
    connect(ui->radioButtonAngle, SIGNAL(clicked(bool)), this, SLOT(showAngles()));
    connect(ui->radioButtonLine, SIGNAL(clicked(bool)), this, SLOT(showLines()));
    connect(ui->radioButtonCircle, SIGNAL(clicked(bool)), this, SLOT(showCircles()));
    connect(ui->pushButtonClipboard, SIGNAL(clicked(bool)),this, SLOT(copyToClipboard()));
    connect(ui->pushButtonSave, SIGNAL(clicked(bool)), this, SLOT(saveToFile()));
}

void MarkDialog::saveToFile()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
                               m_dir,
                               QStringLiteral("Excel (*.csv)"));
    if( !fileName.isEmpty () )
    {
        QDir directory(fileName);
        m_dir = directory.absolutePath();
        QString contents;
        if(ui->radioButtonLine->isChecked())
        {
            contents = linesDataString(',');
        }
        else if(ui->radioButtonCircle->isChecked())
        {
            contents = circlesDataString(',');
        }
        else if(ui->radioButtonAngle->isChecked())
        {
            contents = angleDataString(',');
        }
        QFile file(fileName);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        {
           return;
        }
        QTextStream out(&file);
        out << contents;
    }
}


void MarkDialog::copyToClipboard()
{
    QString contents;
    if(ui->radioButtonLine->isChecked())
    {
        contents = linesDataString('\t');
    }
    else if(ui->radioButtonCircle->isChecked())
    {
        contents = circlesDataString('\t');
    }
    else if(ui->radioButtonAngle->isChecked())
    {
        contents = angleDataString('\t');
    }

    QClipboard *clipboard = QGuiApplication::clipboard();
    clipboard->setText(contents);

}

QString MarkDialog::linesDataString(QChar sep)
{
    int N = m_lines.size();
    QString content = tr(u8"起点 %1 终点 %1 长度 \n").arg(sep);

    for(int i = 0; i < N; i++)
    {
        content += tr("(%1; %2) %3").arg(m_lines.at(i).m_start.x()/1000.0).arg(m_lines.at(i).m_start.y()/1000.0).arg(sep);
        content += tr("(%1; %2) %3").arg(m_lines.at(i).m_end.x()/1000.0).arg(m_lines.at(i).m_end.y()/1000.0).arg(sep);
        content += tr("%1 \n").arg(m_lines.at(i).m_length_um/1000.0);
    }
    return content;
}

QString MarkDialog::circlesDataString(QChar sep)
{
    int N = m_circles.size();
    QString content = tr(u8"圆心 %1 直径\n").arg(sep);
    for(int i = 0; i < N; i++)
    {
        content += tr("(%1; %2) %3").arg(m_circles.at(i).m_center.x() / 1000.0).arg(m_circles.at(i).m_center.y()/1000.0).arg(sep);
        content += tr("%1 \n").arg(m_circles.at(i).m_diameter_um/1000.0);
    }
    return content;
}

QString MarkDialog::angleDataString(QChar sep)
{
    int N = m_angles.size();
    QString content = tr(u8"起点 %1 折点 %1 终点 %1 角度\n").arg(sep);
    for(int i = 0; i < N; i++)
    {
        content += tr("(%1; %2) %3").arg(m_angles.at(i).m_start.x() / 1000.0).arg(m_angles.at(i).m_start.y() / 1000.0).arg(sep);
        content += tr("(%1; %2) %3").arg(m_angles.at(i).m_mid.x() / 1000.0).arg(m_angles.at(i).m_mid.y() / 1000.0).arg(sep);
        content += tr("(%1; %2) %3").arg(m_angles.at(i).m_end.x() / 1000.0).arg(m_angles.at(i).m_end.y() / 1000.0).arg(sep);
        content += tr("%1 \n").arg(m_angles.at(i).m_angle ) ;
    }
    return content;
}


void MarkDialog::showLines()
{
    ui->radioButtonLine->setChecked(true);
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(3);
    QStringList labels;
    labels << u8"长度" << u8"起点" << u8"终点";
    ui->tableWidget->setHorizontalHeaderLabels(labels);

    int N = m_lines.size();
    ui->tableWidget->setRowCount(N);

    for(int i = 0; i < N; i++)
    {
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(tr("%1").arg(m_lines.at(i).m_length_um/1000.0)));
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(tr("(%1, %2)").arg(m_lines.at(i).m_start.x()/1000.0).arg(m_lines.at(i).m_start.y()/1000.0)));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(tr("(%1, %2)").arg(m_lines.at(i).m_end.x()/1000.0).arg(m_lines.at(i).m_end.y()/1000.0)));
    }
}

void MarkDialog::showCircles()
{
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(2);
    QStringList labels;
    labels << u8"圆心" << u8"直径";
    ui->tableWidget->setHorizontalHeaderLabels(labels);

    int N = m_circles.size();
    ui->tableWidget->setRowCount(N);

    for(int i = 0; i < N; i++)
    {
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(tr("(%1, %2)").arg(m_circles.at(i).m_center.x() / 1000.0).arg(m_circles.at(i).m_center.y()/1000.0)));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(tr("%1").arg(m_circles.at(i).m_diameter_um/1000.0)));
    }
}

void MarkDialog::showAngles()
{
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(4);
    QStringList labels;
    labels << u8"起点" << u8"折点" << u8"终点" << u8"角度";
    ui->tableWidget->setHorizontalHeaderLabels(labels);

    int N = m_angles.size();
    ui->tableWidget->setRowCount(N);

    for(int i = 0; i < N; i++)
    {
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(
                                     tr("(%1, %2)").arg(m_angles.at(i).m_start.x() / 1000.0).arg(m_angles.at(i).m_start.y() / 1000.0)
                                     )
                                 );

        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(
                                     tr("(%1, %2)").arg(m_angles.at(i).m_mid.x() / 1000.0).arg(m_angles.at(i).m_mid.y() / 1000.0)
                                     )
                                );
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(
                                     tr("(%1, %2)").arg(m_angles.at(i).m_end.x() / 1000.0).arg(m_angles.at(i).m_end.y() / 1000.0)
                                     )
                                 );
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(
                                     tr("%1").arg(m_angles.at(i).m_angle ) )
                                 );
    }
}

MarkDialog::~MarkDialog()
{
    delete ui;
}

void MarkDialog::addLine(QPointF start, QPointF end, double length)
{
    m_lines.append(LineData(start, end, length));
}

void MarkDialog::addCircle(QPointF center, double dia)
{
    m_circles.append(CircleData(center, dia));
}

void MarkDialog::addAngle(QPointF start, QPointF end, QPointF mid, double angle)
{
    m_angles.append(AngleData(start, mid, end, angle));
}
